package nsu.grandfinal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

// DB Handle Class
public class DatabaseHandle {
	/**
	 * @uml.property  name="db"
	 * @uml.associationEnd  
	 */
	private SQLiteDatabase	db					= null;
	/**
	 * @uml.property  name="consCursor"
	 * @uml.associationEnd  
	 */
	public Cursor						consCursor	= null;
	/**
	 * @uml.property  name="mCtx"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Context					mCtx;
	/**
	 * @uml.property  name="dbHelper"
	 * @uml.associationEnd  readOnly="true"
	 */
	public DatabaseHelper		dbHelper;
	
	public DatabaseHandle(Context ctx) {
		this.mCtx = ctx;
	}
	
	public DatabaseHandle open() throws SQLException {
		db = (new DatabaseHelper(mCtx)).getWritableDatabase();
		consCursor = db.rawQuery("SELECT * FROM setting ORDER BY _id", null);
		return this;
	}
	
	public void createNote(String address, String name, int range, Double lat, Double lon, int mode, int hour, int minute, int day, int alarmcode) {
		ContentValues cv = new ContentValues();
		cv.put("name", name);
		cv.put("address", address);
		cv.put("range", range);
		cv.put("latitude", lat);
		cv.put("longitude", lon);
		cv.put("mode", mode);
		cv.put("hour", hour);
		cv.put("minute", minute);
		cv.put("day", day);
		cv.put("alarmcode", alarmcode);
		db.insert("setting", null, cv);
	}
	
	public void updateNote(int id, String address, String name, int range, Double lat, Double lon, int mode, int hour, int minute, int day, int alarmcode) {
		String[] args = { String.valueOf(id) };
		ContentValues row = new ContentValues();
		row.put("_id", id);
		row.put("name", name);
		row.put("address", address);
		row.put("range", range);
		row.put("latitude", lat);
		row.put("longitude", lon);
		row.put("mode", mode);
		row.put("hour", hour);
		row.put("minute", minute);
		row.put("day", day);
		row.put("alarmcode", alarmcode);
		db.update("setting", row, "_id=?", args);
	}
	
	public int readId(long id) throws SQLException {
		consCursor = db.query("setting", new String[] { "_id" }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		int _id = consCursor.getInt(0);
		return _id;
	}
	
	public String readName(long id) throws SQLException {
		consCursor = db.query("setting", new String[] { "_id", "name" }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		String str = consCursor.getString(1);
		return str;
	}
	
	public String readAddress(long id) throws SQLException {
		consCursor = db.query("setting", new String[] { "_id", "address" }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		String str = consCursor.getString(1);
		return str;
	}
	
	public int readRange(long id) throws SQLException {
		consCursor = db.query("setting", new String[] { "_id", "range" }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		int range = consCursor.getInt(1);
		return range;
	}
	
	public Double readLat(long id) throws SQLException {
		consCursor = db.query("setting", new String[] { "_id", "latitude" }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		Double lati = consCursor.getDouble(1);
		return lati;
	}
	
	public Double readLon(long id) throws SQLException {
		consCursor = db.query("setting", new String[] { "_id", "longitude" }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		Double longi = consCursor.getDouble(1);
		return longi;
	}
	
	public DatabaseHandle close() {
		db.close();
		return this;
	}
	
	public int getLastId() throws SQLException {
		consCursor = db.rawQuery("select _id from setting order by _id desc", null);
		consCursor.moveToFirst();
		return consCursor.getInt(0);
	}
	
	public int getIntData(long id, String select) throws SQLException {
		consCursor = db.query("setting", new String[] { select }, "_id=" + id, null, null, null, null);
		consCursor.moveToFirst();
		return consCursor.getInt(0);
	}
	
	public void dropTable() throws SQLException {
		db = (new DatabaseHelper(mCtx)).getWritableDatabase();
		db.execSQL("DROP TABLE SETTING");
		db.execSQL("CREATE TABLE setting (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT, range INTEGER, latitude REAL, longitude REAL, mode INTEGER, day INTEGER, hour INTEGER, minute INTEGER, alarmcode INTEGER);");
		db.close();
	}
}
